package net.xdclass.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.exception.BizException;
import net.xdclass.model.AddressDO;
import net.xdclass.request.AddressRequest;
import net.xdclass.service.AddressService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.AddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 电商-公司收发货地址表 前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2021-03-12
 */
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1/")
@Slf4j
public class AddressController {
    @Autowired
    private AddressService addressService;

    @ApiOperation("根据id查询地址详情")
    @GetMapping("find/{id}")
    public Object find(@ApiParam(value="地址id",required = true)
                           @PathVariable("id") Long id){
        AddressVO addressVO = addressService.find(id);
        return addressVO==null?JsonData.buildResult(BizCodeEnum.ADDRESS_NOID):JsonData.buildSuccess(addressVO);
    }

    @ApiOperation("保存地址信息")
    @PostMapping("save")
    public Object save(@ApiParam(value="地址",required = true)
                       @RequestBody AddressRequest addressRequest){
        addressService.save(addressRequest);
        return JsonData.buildSuccess();
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping("delete/{id}")
    public Object delete(@ApiParam(value="地址id",required = true)
                       @PathVariable("id") Long id){
        int rows = addressService.del(id);
        return rows==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.ADDRESS_DEL_FAIL);
    }

    @ApiOperation("获取用户的所有收货地址")
    @GetMapping("list")
    public Object findAddressList(){
        List<AddressVO> list = addressService.findAddressList();
        return JsonData.buildSuccess(list);
    }
}

